import { size, IFormProps, pageSize, dateTimeFormat, dateFormat } from "../../utils/common";
import React from "react";
import { connect } from "dva";
import { Form, Row, Col, Button, Input, Table, Popconfirm, Select, DatePicker, Tabs, message } from 'antd';
import { PaginationProps } from "antd/lib/pagination";
const Styles = require("../../styles/index.less");
import * as api from '../../api/api';
import moment from 'moment';

export interface IChargeOrdersPros extends IFormProps {
    order: any
}
class ChargeOrders extends React.PureComponent<IChargeOrdersPros, any>{
    constructor(props) {
        super(props);
    }

    state = {
        beginTime: '',
        endTime: '',
        visible: false,
        status: ''
    }

    componentDidMount() {
        this.search();
    }

    search = () => {
        this.getChargeOrders({ pageIndex: 1 });
    }

    getChargeOrders = (payload?) => {
        const { beginTime, endTime } = this.state;
        payload = Object.assign({},
            this.props.order.chargeOrderSearchs,
            this.props.form.getFieldsValue(),
            { beginTime, endTime },
            { ...payload });
        this.props.dispatch({
            type: "order/getChargeOrders",
            payload
        });
    }

    reset = () => {
        this.props.form.resetFields();
    }

    onChange = (date, dateString) => {
        this.setState({ beginTime: dateString[0], endTime: dateString[1] });
    }

    OnPayTypeChange = (payType) => {
        let payload = Object.assign({}, { payType, pageIndex: 1 });
        this.getChargeOrders(payload);
    }

    OnOrderTypeChange = (orderType) => {
        let payload = Object.assign({}, { orderType, pageIndex: 1 });
        this.getChargeOrders(payload);
    }

    render(): JSX.Element {
        const { getFieldDecorator } = this.props.form;
        const { loading } = this.props;
        const {
            chargeOrders: { totalCount, items },
            chargeOrderSearchs: { pageIndex, pageSize } } = this.props.order;

        const pageChange = (pageIndex, pageSize) => {
            let payload = Object.assign({},
                this.props.order.chargeOrderSearchs,
                { pageIndex, pageSize });
            this.getChargeOrders(payload);
        }
        const pagination: PaginationProps | boolean = {
            total: totalCount,
            defaultCurrent: 1,
            current: pageIndex,
            pageSize: pageSize,
            showSizeChanger: true,
            showTotal: (total) => `共 ${total} 条`,
            showQuickJumper: true,
            pageSizeOptions: ["10", "20", "30", "50"],
            onShowSizeChange: pageChange,
            onChange: pageChange
        };
        return (
            <Row className={Styles.F_BG_WHITE1}>
                <Row className={Styles.F_BG_WHITE}>
                    <Col span={24}>
                        <Form layout="inline">
                            <Form.Item label='订单号'>
                                {getFieldDecorator('orderNo', {
                                })(<Input size={size}
                                    placeholder="请输入订单号" />)}
                            </Form.Item>
                            <Form.Item label='支付人姓名或电话'>
                                {getFieldDecorator('keywords', {
                                })(<Input size={size}
                                    placeholder="请输入姓名或电话" />)}
                            </Form.Item>
                            <Form.Item label='支付时间'>
                                <DatePicker.RangePicker onChange={this.onChange}
                                    format={dateFormat} />
                            </Form.Item>
                            <Form.Item label='支付来源'>
                                {getFieldDecorator('payType', {
                                })(<Select style={{ width: 120 }} onChange={this.OnPayTypeChange}>
                                    <Select.Option value=''>全部</Select.Option>
                                    <Select.Option value='1'>微信公众号</Select.Option>
                                    <Select.Option value='6'>佣金支付</Select.Option>
                                    <Select.Option value='7'>体验金支付</Select.Option>
                                    <Select.Option value='5'>微信APP</Select.Option>
                                    <Select.Option value='2'>支付宝App</Select.Option>
                                </Select>)}
                            </Form.Item>
                            <Form.Item label='充值类型'>
                                {getFieldDecorator('orderType', {
                                    initialValue: '4'
                                })(<Select style={{ width: 120 }} onChange={this.OnOrderTypeChange}>
                                    <Select.Option value=''>全部</Select.Option>
                                    <Select.Option value='2'>钱包充值</Select.Option>
                                    <Select.Option value='4'>代理充值</Select.Option>
                                </Select>)}
                            </Form.Item>
                            <Form.Item>
                                <Button size={size} type="primary" loading={loading} onClick={this.search}>查询</Button>
                            </Form.Item>
                        </Form>
                        <Col span={24}>
                            <hr className={Styles.hrBorder} />
                            <h3>充值订单列表 (共 {totalCount} 条)</h3>
                        </Col>
                        <Col span={24}>
                            {totalCount > 0 ? <Table
                                dataSource={items}
                                pagination={pagination}
                                rowKey={(record: api.SingleUserMallOrdersVOOrderChargeVO) => String(record.id)}
                                loading={loading}>
                                <Table.Column title="订单编号" key="orderNo" dataIndex="orderNo" />
                                <Table.Column title="支付人姓名" key="userName" dataIndex="userName" />
                                <Table.Column title="支付人电话" key="userPhoneNumber" dataIndex="userPhoneNumber" />
                                <Table.Column title="支付总额" key="actualAmount" dataIndex="actualAmount" className={Styles.redColor} />
                                <Table.Column title="支付时间" key="time" render={(text, record: api.SingleUserMallOrdersVOOrderChargeVO, index) => {
                                    return moment(record.payTime).format(dateTimeFormat)
                                }} />
                                <Table.Column title="充值类型" key="orderTypeView" dataIndex="orderTypeView" />
                                <Table.Column title="状态" key="statusView" dataIndex="statusView" />
                            </Table> : ""}
                        </Col>
                    </Col>
                </Row>
            </Row>)
    }
}

export default connect(({ order, loading: { models: { order: loading } } }) => ({ order, loading }))(Form.create()(ChargeOrders));
